//
//  Archiver.h
//  Runtime归档解档
//
//  Created by youmy on 2018/1/15.
//  Copyright © 2018年 youmy. All rights reserved.
//

#define ArchiverM(className)\
- (void)encodeWithCoder:(NSCoder *)encoder{\
    unsigned int count = 0;\
    Ivar * ivars = class_copyIvarList([className class], &count);\
    for (int i = 0; i < count; i++) {\
        Ivar ivar = ivars[i];\
        const char * name = ivar_getName(ivar);\
        NSString * key = [NSString stringWithUTF8String:name];\
        id value = [self valueForKey:key];\
        [encoder encodeObject:value forKey:key];\
    }\
    free(ivars);\
}\
- (instancetype)initWithCoder:(NSCoder *)decoder{\
    if (self = [super init]) {\
        unsigned int count = 0;\
        Ivar * ivars = class_copyIvarList([className class], &count);\
        for (int i = 0; i < count; i++) {\
            Ivar ivar = ivars[i];\
            const char * name = ivar_getName(ivar);\
            NSString * key = [NSString stringWithUTF8String:name];\
            id value = [decoder decodeObjectForKey:key];\
            [self setValue:value forKey:key];\
        }\
        free(ivars);\
    }\
    return self;\
}
